package com.example.studentgradesmanagement.service;

import com.example.studentgradesmanagement.model.Grade;
import com.example.studentgradesmanagement.model.Student;
import com.example.studentgradesmanagement.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;

    public List<Student> listAll() {
        return studentRepository.findAll();
    }

    public Optional<Student> getById(Long studentId) {
        return studentRepository.findById(studentId);
    }

    public void add(Student student) {
        studentRepository.save(student);
    }

    public List<Grade> getGrades(Long studentId) {
        return studentRepository.findById(studentId)
                .map(Student::getGrades)
                .orElse(null);
    }
}

